package com.lanou3g.code0522.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/22;
 * Otherwise, I don't know who created it either
 */

/*
Listener 是JavaWeb提供的3大组件之一
它是用来在特定时间来提供回调的
我们可以通过创建不同种类的listener,来实现,在不同情况下
调用我们自己写的代码
Listener与Servlet一样需要注册
注册方式1. 在web.xml中 通过
listener标签来注册即可

ServletContextListener
会先于任何的 servlet/Filter 执行

javaWeb 提供的Listener
按照 被监听的对象分类
1. ServletContext
    1.1 ServletContextListener
    1.2 ServletContextAttributeListener
2. HttpSession
    2.1 HttpSessionListener
    2.2 HttpSessionAttributeListener

    感知监听器
    2.3 HttpSessionBindingListener
    2.4 HttpSessionActivationListener
3. ServletRequest
    3.1 ServletRequestListener
    3.2 ServletRequestAttributeListener
 */
public class FirstListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized");
        ServletContext app = sce.getServletContext();
        app.setAttribute("listener","listener");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed");
    }
}
